<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_AwardController extends Core_Controller_Action_Standard {
	protected $_navigation;
	
	public function init() {
		if (! $this->_helper->requireAuth ()->setAuthParams ( 'badge', null, 'view' )->isValid ())
			return;
		
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($award_id = ( int ) $this->_getParam ( 'award_id' )) && null !== ($award = Engine_Api::_ ()->getItem ( 'badge_award', $award_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $award );
			} else if (0 !== ($user_id = ( int ) $this->_getParam ( 'user_id' )) && null !== ($user = Engine_Api::_ ()->getItem ( 'user', $user_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $user );
			}
		}
		
		$this->_helper->requireUser->addActionRequires ( array ()

		 );
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('view' => 'badge_award', 'list' => 'user' ) );
	}
	
	public function browseAction() {
		// Get navigation
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_main' );
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->form = $form = new Badge_Form_Filter_Award ();
		
		$values = array ();
		// Populate form data
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
		}
		
		$values = Engine_Api::_ ()->getApi ( 'filter', 'radcodes' )->removeKeyEmptyValues ( $values );
		$this->view->formValues = $values;
		
		$this->view->assign ( $values );
		
		$values ['limit'] = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'badge.awardperpage', 10 );
		$values ['active'] = true;
		$values ['search'] = 1;
		
		$this->view->paginator = $paginator = Engine_Api::_ ()->badge ()->getAwardsPaginator ( $values );
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
	}
	
	public function viewAction() {
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->award = $award = Engine_Api::_ ()->core ()->getSubject ( 'badge_award' );
		$this->view->user = $user = $award->getUser ();
		$this->view->badge = $badge = $award->getBadge ();
		
		$this->view->awardPaginator = $paginator = Engine_Api::_ ()->badge ()->getAwardsPaginator ( array ('order' => 'recent', 'user' => $award->getUser (), 'active' => true, 'exclude_award_ids' => array ($award->getIdentity () ), 'limit' => 3 ) );
		
		if (! $user->isSelf ( $viewer )) {
			$award->view_count ++;
			$award->save ();
		}
	
	}
	
	public function listAction() {
		$this->view->user = $user = Engine_Api::_ ()->core ()->getSubject ( 'user' );
		
		$this->view->paginator = $paginator = Engine_Api::_ ()->badge ()->getAwardsPaginator ( array ('order' => 'recent', 'user' => $user, 'active' => true, 'limit' => 999 ) );
	
	}

}

